package thread;

/**
 * 守护线程
 * 守护线程是通过调用线程的setDaemon(true)设置转变的.
 * 正常常见线程时,默认都是用户线程
 */
public class DaemonThreadDemo {
    public static void main(String[] args) {
        Thread rose = new Thread(){
            @Override
            public void run() {
                for (int i =0;i<5;i++){
                    System.out.println("rose:let me go!");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println("rose:啊啊啊啊啊AAAAAaaaaaa...");
            }
        };
        Thread jack = new Thread(){
            @Override
            public void run() {
                 while(true){
                     System.out.println("you jump,I jump!");
                     try {
                         Thread.sleep(1000);
                     } catch (InterruptedException e) {
                         throw new RuntimeException(e);
                     }
                 }
            }
        };
        rose.start();
        //设置守护线程的工作必须在该线程启动前进行
        jack.setDaemon(true);
        jack.start();

//        System.out.println("主线程结束了");

        while (true);//如果主线程不结束,rose线程结束了,守护线程jack也不会结束
    }
}
